package user

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	microGfyxUserService "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
)

type userRepository struct{}

// NewUserRepository
// 构造函数
func NewUserRepository() UserRepositoryIface {
	return &userRepository{}
}

func (r *userRepository) UserLogout(ctx context.Context, token string) error {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := microGfyxUserService.NewUserLoginServiceClient(conn)
	_, err = client.Logout(ctx, &microGfyxUserService.LogoutRequest{
		Token: token,
	})
	if err != nil {
		return err
	}
	return nil
}

func (r *userRepository) SsoLogin(ctx context.Context, accessToken string) (string, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return "", err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := microGfyxUserService.NewUserLoginServiceClient(conn)
	res, err := client.LoginBySso(ctx, &microGfyxUserService.LoginBySsoRequest{
		AccessToken: accessToken,
	})
	if err != nil {
		return "", err
	}

	if res == nil {
		return "", errors.New("登录失败")
	}

	return res.Token, nil
}

func (r *userRepository) UserLogin(ctx context.Context, tenantCode, account, password string) (*UserLoginInfo, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxUserService.NewUserLoginServiceClient(conn)
	res, err := client.LoginByAccountPassword(ctx, &microGfyxUserService.LoginByAccountPasswordRequest{
		TenantCode: tenantCode,
		Account:    account,
		Password:   password,
	})
	if err != nil {
		return nil, err
	}
	if res == nil {
		return nil, errors.New("登录失败")
	}

	unionUserLoginInfo := UserLoginInfo{}
	if res.UserInfo != nil {
		unionUserLoginInfo.UserInfo = &UserInfo{
			UserName:      res.UserInfo.UserName,
			Uid:           res.UserInfo.Uid,
			UserCode:      res.UserInfo.UserCode,
			TenantUserUid: res.UserInfo.UserUid,
			//Logo:       res.UserInfo.Logo,
			Mobile: res.UserInfo.MobilePhone,
		}
		if res.TenantInfo != nil && len(res.TenantInfo) > 0 {
			unionUserLoginInfo.UserInfo.TenantCode = res.TenantInfo[0].GetTenantCode()
			unionUserLoginInfo.UserInfo.TenantName = res.TenantInfo[0].GetTenantName()
		}
	}

	unionUserLoginInfo.Token = res.Token

	return &unionUserLoginInfo, nil
}

// GetUserInfo
// 获取用户信息
func (r *userRepository) GetUserInfo(ctx context.Context, params *GetUserInfoParams) (*UserLoginInfo, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxUserService.NewUserInfoServiceClient(conn)
	res, err := client.GetUserInfo(ctx, &microGfyxUserService.GetUserInfoRequest{
		Type:  2,
		Param: params.GlobalUid,
	})
	if err != nil {
		return nil, err
	}
	if res == nil {
		return nil, nil
	}
	loginInfo := &UserLoginInfo{}
	if res.UserInfo != nil {
		loginInfo.UserInfo = &UserInfo{
			UserName: res.UserInfo.UserName,
			UserCode: res.UserInfo.UserCode,
			// todo
			Logo:          "",
			Uid:           res.UserInfo.Uid,
			TenantUserUid: res.UserInfo.UserUid,
			Mobile:        res.UserInfo.MobilePhone,
			//UserId:        res.UserInfo.UserUid,
			//MpOpenId:      res.UserInfo.MpOpenId,
			//GlobalUserUid: res.UserInfo.Uid,
		}
	}
	if len(res.TenantInfo) != 0 {
		loginInfo.UserInfo.TenantCode = res.TenantInfo[0].TenantCode
		loginInfo.UserInfo.TenantName = res.TenantInfo[0].TenantName
	}
	return loginInfo, nil
}

// VerifyToken
// 验证token
func (r *userRepository) VerifyToken(ctx context.Context, token string) (info *UserInfo, tenantInfo *TenantUnionInfo, err error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxUserService.NewUserLoginServiceClient(conn)
	res, err := client.VerifyToken(ctx, &microGfyxUserService.VerifyTokenRequest{Token: token})
	if err != nil {
		return nil, nil, err
	}
	if res == nil {
		return nil, nil, nil
	}

	userInfo := &UserInfo{}
	if res.UserInfo != nil {
		userInfo.Uid = res.UserInfo.Uid
		userInfo.UserName = res.UserInfo.UserName
		userInfo.UserCode = res.UserInfo.UserCode
		userInfo.TenantUserUid = res.UserInfo.UserUid
		userInfo.Mobile = res.UserInfo.MobilePhone
	}

	tenantInfo = &TenantUnionInfo{}
	if len(res.TenantInfo) != 0 {
		userInfo.TenantName = res.TenantInfo[0].GetTenantName()
		userInfo.TenantCode = res.TenantInfo[0].GetTenantCode()
	}

	return userInfo, tenantInfo, nil
}
